package com.burgeon.backend.SHOP.controller;

import com.burgeon.backend.SHOP.entity.PayData;
import com.burgeon.backend.SHOP.mappers.GoodsMapper;
import com.burgeon.backend.SHOP.service.GroupOrderService;
import com.burgeon.backend.SHOP.service.ex.OrderExceptionApp;
import com.burgeon.backend.SHOPBACKEND.rest.GetGoodsCode;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

@RestController
@CrossOrigin
@RequestMapping("shop/groupOrder")
public class GroupOrderController extends BaseController {

    @Autowired
    private GroupOrderService groupOrderService;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 团购商品支付结果页面
     *
     * @param gid     商品 id
     * @param oid     订单 id
     * @param session
     * @return
     */
    @RequestMapping("result")
    public ResponseResult<HashMap> groupOrderResult(String gid, String oid, HttpSession session) {

        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        HashMap data = groupOrderService.orderResult(gid, oid, uid, token);
        return new ResponseResult<HashMap>(SUCCESS, data);
    }

    @RequestMapping("add")
    public ResponseResult<PayData> addGroupOrder(String grid, String groupOrderId, String aid, String mid,
                                                 String cid, String code, String color,
                                                 String size, @RequestParam(value = "comment", required = false, defaultValue = " ") String comment,
                                                 String emp, HttpSession session) {
        if (null == session.getAttribute("uid")) {
            return new ResponseResult<>(NOTLOG);
        }
        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        String phone = session.getAttribute("phone").toString();
        String barcode = goodsMapper.getSizeCodeByCodeColorSize(code,color,size);

        if (null == barcode){
            Info info = accountMapper.GetTokenData(token);
            GetGoodsCode getGoodsCode = new GetGoodsCode(info.getUrl(), info.getAccount(), info.getPassword());
            try {
                code = getGoodsCode.getData(code, color, size);
            } catch (Exception e) {
                e.printStackTrace();
                throw new OrderExceptionApp("出现错误");
            }
        }else {
            code = barcode;
        }

        PayData data = groupOrderService.addGroupOrder
                (grid, groupOrderId, mid, uid, aid, cid, code, phone, color + "-" + size, openid, token, comment,emp);
        return new ResponseResult<PayData>(SUCCESS, data);
    }


    @RequestMapping("create")
    public ResponseResult<PayData> createGroupOrder(String grid, String aid, String mid,
                                                    String cid, String code, String color,
                                                    String size, @RequestParam(value = "comment", required = false, defaultValue = " ") String comment,
                                                    String emp, HttpSession session) {
        if (null == session.getAttribute("uid")) {
            return new ResponseResult<>(NOTLOG);
        }
        System.err.println("grid=" + grid + "aid=" + aid + "mid=" + mid + "cid=" + cid + "code=" + code + "color=" + color + "size=" + size);
        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        String phone = session.getAttribute("phone").toString();
        String barcode = goodsMapper.getSizeCodeByCodeColorSize(code,color,size);

        if (null == barcode){
            Info info = accountMapper.GetTokenData(token);
            GetGoodsCode getGoodsCode = new GetGoodsCode(info.getUrl(), info.getAccount(), info.getPassword());
            try {
                code = getGoodsCode.getData(code, color, size);
            } catch (Exception e) {
                e.printStackTrace();
                throw new OrderExceptionApp("出现错误");
            }
        }else {
            code = barcode;
        }
        PayData data = groupOrderService.createGroup(grid, mid, uid, aid, cid, code, phone, color + "-" + size, openid, token, comment, emp);
        return new ResponseResult<PayData>(SUCCESS, data);
    }


}
